package cn.edu.zut.curriculum.controller;

import cn.edu.zut.curriculum.entity.Commodity;
import cn.edu.zut.curriculum.entity.Order;
import cn.edu.zut.curriculum.entity.User;
import cn.edu.zut.curriculum.service.ICommodityService;
import cn.edu.zut.curriculum.service.IOrderSerivice;
import cn.edu.zut.curriculum.service.IUserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.jws.soap.SOAPBinding;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class CommodityController {

    @Autowired
    ICommodityService iCommodityService;
    @Autowired
    IOrderSerivice iOrderSerivice;
    @Autowired
    IUserService iUserService;

    @PostMapping("/searchResult{uid}")
    public ModelAndView select(String name, ModelAndView mav,@RequestParam long uid) {
       // System.out.println(name);
      List <Commodity> list = iCommodityService.select(name);
      User user=iUserService.select(uid);
      //List<String> list1=null;
        if (list == null){
            System.out.println("1");
            mav.setViewName("/User/list");
            mav.addObject("message","未查找到商品，请重新输入");
        }
        else {
            for( Commodity commodity:list) {
                System.out.println(commodity.getName());
                System.out.println(commodity.getIntroduce());
                System.out.println(commodity.getAvatar1());
                //list1.add(commodity.getAvatar1());
            }
            mav.addObject("list",list);
            mav.addObject("user",user);
            mav.setViewName("User/searchResult");
        }
        return mav;
    }
    /*
    @PostMapping("skip")
    public ModelAndView skip(ModelAndView mav){
        mav.setViewName("/User/publish");
        return mav;
    }
     */



    @GetMapping("/commodity{cid}{uid}")     // 点进去商品
    public ModelAndView skip3(ModelAndView mav,@RequestParam("cid") long cid,@RequestParam("uid") long uid){
        System.out.println(uid+""+cid);
        Commodity commodity=iCommodityService.select(cid);  //读取该商品所有信息
        mav.addObject("commodity",commodity);
        mav.addObject("uid",uid);
        mav.setViewName("/User/commodity");
        return mav;
    }

    @PostMapping("/publish")   //发布商品
    public ModelAndView publish(@RequestParam("avator1") MultipartFile file, Commodity commodity, ModelAndView mav,@RequestParam("id") long id){

        User user=iUserService.select(id);
        if(user.getStatus()==0){
            mav.addObject("message","已被封禁，不能上传");
            mav.addObject("user",user);
            mav.setViewName("/User/list");
            return mav;
        }
        String filename = file.getOriginalFilename();
        System.out.println("上传的文件名："+filename);
        String serviceRoot ="E:\\learnjava\\Curriculum-Setting\\curriculum-design\\src\\main\\resources\\static\\";
        String path ="assets\\images\\";
        File filepath = new File(serviceRoot+path, filename);
        // 判断路径是否存在，如果不存在就创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }try {
            // 写入文件
            file.transferTo(new File(serviceRoot+path +filename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //数据库中记下文件相对于与Server的服务地址
        commodity.setAvatar1(path+filename);
        commodity.setUid(id);

        System.out.println(commodity.getName());
        commodity= iCommodityService.insert(commodity);
        if(commodity.getMessage()!=null){
            mav.addObject("message",commodity.getMessage());
            mav.setViewName("/User/publish");
            return mav;
        }

        mav.setViewName("/User/publish");
        System.out.println("cid="+commodity.getId());
        mav.addObject("user",user);
        mav.addObject("cid",commodity.getId());
        mav.addObject("message","上架成功");
        return mav;
    }

    @PostMapping("/buy")   //下单  并对商品表中状态改为已售出
    public ModelAndView buy(ModelAndView mav, @RequestParam("cid") long cid, @RequestParam("uid") long uid, Order order){
        System.out.println("uid="+uid+"cid="+cid);
        Commodity commodity=iCommodityService.select(cid);  //查询商品信息
        order.setSuid(commodity.getUid());        //向订单表传递数据
        order.setBuid(uid);
        LocalDateTime now = LocalDateTime.now();  //得到当前时间
        order.setBuyTime(now);
        order=iOrderSerivice.insert(order);
        User user=iUserService.select(uid);
        int i=iCommodityService.update(cid);
        if(i==1){
            mav.addObject("user",user);     //传到前端用户信息
            mav.addObject("message","下单成功");
        }

        mav.setViewName("/User/list");
        return mav;
    }


    @GetMapping("/inidle")         //    自己的闲置物品
    public ModelAndView my(ModelAndView mav,@RequestParam("uid")long uid){
        List<Commodity>list=iCommodityService.select3(uid);
        User user=iUserService.select(uid);
        mav.addObject("user",user);
        mav.addObject("list",list);
        mav.setViewName("/User/inidle");
        return mav;
    }

    @PostMapping("/changeprice")  //价格更改
    public ModelAndView changeprice(String price,ModelAndView mav,@RequestParam("uid")long uid,@RequestParam("cid") long cid){
        int i=iCommodityService.update3(price,cid);
        User user=iUserService.select(uid);
        System.out.println(i);
        if(i==1){
            mav.addObject("message","修改成功");
        }
        else
            mav.addObject("message","未知错误");
        mav.addObject("user",user);
        mav.setViewName("/User/list");
        return mav;
    }
/*
    @PostMapping("/userdown")  //管理员下架操作
    public ModelAndView down(ModelAndView mav,@RequestParam("cid")long cid,@RequestParam("uid") long uid) {
        User user=iUserService.select(uid);
        int i=iCommodityService.delete(cid);
        if(i==1){
            mav.addObject("message","已成功下架");

        }
        else
            mav.addObject("message","未知异常");


        mav.addObject("administrator",user);
        mav.setViewName("/User/list");
        return mav;
    }

 */


}
